package com.ifgi.sitcom.campusmapperNDrawer.controllers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.gamificationlibrary.model.leaderboard.Leaderboard;
import com.gamificationlibrary.model.*;
import com.ifgi.sitcom.campusmapperNDrawer.gamedata.PlayerAdapter;
import com.ifgi.sitcom.campusmapperNDrawer.gamedata.ScorePerFloor;
import com.ifgi.sitcom.campusmapperNDrawer.gamedata.SerialBitmap;
import com.ifgi.sitcom.campusmapperNDrawer.indoordata.FloorPlan;
import com.ifgi.sitcom.campusmapperNDrawer.io.*;
import com.ifgi.sitcom.campusmapperNDrawer.outdoordata.Building;


public class PlayerController {
	
	private static PlayerController instance=null;
	private PlayerAdapter currentPlayer;

    public static PlayerController getInstance() {
       if(instance==null)
    	instance=new PlayerController();
	return instance;
    }

	public PlayerAdapter createPlayer(String username,SerialBitmap result,String IDPlus)
	{
		currentPlayer = new PlayerAdapter(username,result,IDPlus);
		return currentPlayer;
	}
	
	public PlayerAdapter createPlayer(Player p)
	{
		currentPlayer = (PlayerAdapter) p;
		return currentPlayer;
	}
	
	public void writePlayerDB(PlayerAdapter player)
	{
		RDFUpload rdfupload=new RDFUpload();
		rdfupload.uploadePlayer(player);
	}
		
	public void deleteCurrentPlayerDB()
	{
		RDFDeleteAndUpdate rDFDeleteAndUpload =  new RDFDeleteAndUpdate();
		rDFDeleteAndUpload.deletePlayer(currentPlayer);	
	}
	
	
	/*public PlayerAdapter readPlayer()
	{
		return currentPlayer;	
	}*/

	public void createScorePerFloorPlan(FloorPlan mFloorPlan,Score scorefloor) {
		// TODO Auto-generated method stub
		ScorePerFloor score=new ScorePerFloor();
		score.setFloor(mFloorPlan);
		score.setScore(scorefloor);
		score.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).format(new Date()));
		Log.v("username",currentPlayer.getEmail());
		currentPlayer.setScoreFloor(score);
	}
	
	public void uploadScorePerFloorDB()
	{
		RDFUpload rdfupload=new RDFUpload();
		FloorPlan floor=currentPlayer.getScoreFloor().getFloor();
		Log.v("building",floor.getBuildingURI());
		Log.v("floornumber",String.valueOf(floor.getFloorNumber()));
		String floorUri="";
		if(floor.getFloorURI()!=null)
			floorUri= floor.getFloorURI();
		else floorUri=floor.getBuildingURI()+"/floor/"+String.valueOf(floor.getFloorNumber());
			
		rdfupload.uploadScorePerFloorPlan(currentPlayer.getEmail(),floorUri, currentPlayer.getScoreFloor().getScore().getValue(),currentPlayer.getScoreFloor().getDate());
	}
	
	public void deleteAndUpdate_totalScoreDB()
	{
		RDFDeleteAndUpdate rdfupload=new RDFDeleteAndUpdate();
		rdfupload.deleteAndUpdate_totalScore(currentPlayer.getEmail(),currentPlayer.getScoreTotal().getValue());
	}

}
